package com.zxj.transfer.orm.service;

import com.zxj.transfer.orm.dao.UserAccountFreezeMapper;
import com.zxj.transfer.orm.dao.UserAccountMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Service
public class UserAccountService {

    @Resource
    private UserAccountMapper userAccountMapper;
    @Resource
    private UserAccountFreezeMapper userAccountFreezeMapper;
    @Resource
    private TTryLogService tryLogService;
    @Resource
    private TConfirmLogService confirmLogService;
    @Resource
    private TCancelLogService cancelLogService;

    @Transactional
    public void subtractUsAccount(Integer userId, String tid, BigDecimal usAccount) {
        userAccountMapper.subtractUsAccount(userId, usAccount);
        userAccountFreezeMapper.addAccountFreeze(userId, tid, BigDecimal.ZERO, usAccount);
        tryLogService.insert(tid);
    }

    @Transactional
    public void confirmSubtractUsAccount(Integer userId, String tid, BigDecimal rmbAccount) {
        userAccountMapper.addRmbAccount(userId, rmbAccount);
        userAccountFreezeMapper.updateStatus(tid);
        confirmLogService.insert(tid);
    }

    @Transactional
    public void cancelSubtractUsAccountNoConfirm(Integer userId, String tid, BigDecimal usAccount) {
        userAccountMapper.addUsAccount(userId, usAccount);
        userAccountFreezeMapper.updateStatus(tid);
        cancelLogService.insert(tid);
    }

    @Transactional
    public void cancelSubtractUsAccountConfirm(Integer userId, String tid, BigDecimal usAccount, BigDecimal rmbAccount) {
        userAccountMapper.subtractRmbAccount(userId, rmbAccount);
        userAccountMapper.addUsAccount(userId, usAccount);
        userAccountFreezeMapper.updateStatus(tid);
        cancelLogService.insert(tid);
    }

    /******************************************************/

    @Transactional
    public void subtractRmbAccount(Integer userId, String tid, BigDecimal rmbAccount) {
        userAccountMapper.subtractRmbAccount(userId, rmbAccount);
        userAccountFreezeMapper.addAccountFreeze(userId, tid, rmbAccount, BigDecimal.ZERO);
        tryLogService.insert(tid);
    }

    @Transactional
    public void confirmSubtractRmbAccount(Integer userId, String tid, BigDecimal usAccount) {
        userAccountMapper.addUsAccount(userId, usAccount);
        userAccountFreezeMapper.updateStatus(tid);
        confirmLogService.insert(tid);
    }

    @Transactional
    public void cancelSubtractRmbAccountNoConfirm(Integer userId, String tid, BigDecimal rmbAccount) {
        userAccountMapper.addRmbAccount(userId, rmbAccount);
        userAccountFreezeMapper.updateStatus(tid);
        cancelLogService.insert(tid);
    }

    @Transactional
    public void cancelSubtractRmbAccountConfirm(Integer userId, String tid, BigDecimal usAccount, BigDecimal rmbAccount) {
        userAccountMapper.subtractUsAccount(userId, usAccount);
        userAccountMapper.addRmbAccount(userId, rmbAccount);
        userAccountFreezeMapper.updateStatus(tid);
        cancelLogService.insert(tid);
    }
}
